package com.hertz.iris.receiver;

import java.util.List;

import javax.inject.Inject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.hertz.iris.Injector;
import com.hertz.iris.core.MatchAlarmService;
import com.hertz.iris.core.NormalAlarmService;
import com.hertz.iris.core.TriggerService;
import com.hertz.iris.pojo.MatchAlarm;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.util.Constants;
import com.hertz.iris.util.Ln;

public class BootBroadcastReceiver extends BroadcastReceiver {

    @Inject protected TriggerService triggerService;
    @Inject protected MatchAlarmService matchALarmService;
    @Inject protected NormalAlarmService normalAlarmService;
    @Override
    public void onReceive(Context context, Intent intent) {
        Ln.d("Boot this system , BootBroadcastReceiver onReceive()");
        Injector.inject(this);
        //normal alarm
        List<NormalAlarm> normalAlarmList = normalAlarmService.loadAllNormalAlarm();
        if(normalAlarmList != null){
        	for(NormalAlarm item : normalAlarmList){
        		if(item.getIsActive()){
        			normalAlarmService.resetAlarmActive(item);
        		}
        	}
        }
        //match alarm
//        triggerService.matchAlarmAlertTrigger(System.currentTimeMillis() + Constants.DEFAULT_INTERVAL);
        List<MatchAlarm> list = matchALarmService.getMatchAlarmFromLocalByStatus(Constants.MATCH_ALARM.STATUS_UNALERM);
        if(list != null){
        	long currentTime = System.currentTimeMillis();
        	for(MatchAlarm ma : list){
        		long matchTime = ma.getMatchTime();
        		if(currentTime > matchTime){
        			matchALarmService.createMatchAlarmTrigger(ma, currentTime + Constants.DEFAULT_INTERVAL);
        		}else{
        			matchALarmService.createMatchAlarmTrigger(ma, matchTime);
        		}
        	}
        }
    }
}
